//
//  DCUMShareTool.m
//  DCProject
//
//  Created by bigbing on 2019/3/31.
//  Copyright © 2019 bigbing. All rights reserved.
//

#import "DCUMShareTool.h"


#if __has_include(<UShareUI/UShareUI.h>) && __has_include(<UMShare/UMShare.h>) && __has_include("WXApi.h")
@implementation DCUMShareTool


#pragma mark - 单列
+ (DCUMShareTool *) shareClient{
    static DCUMShareTool * _instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[[self class] alloc] init];
    });
    return _instance;
}


#pragma mark - 分享
- (void)shareInfoWithTitle:(NSString *)title content:(NSString *)content url:(NSString *)url
{
    // 自定义面板
    [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewBackgroundColor = [UIColor dc_colorWithHexString:@"#E6E6E6"];
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.shareContainerBackgroundColor = [UIColor dc_colorWithHexString:@"#E6E6E6"];
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.shareContainerMarginLeft = 0;
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.shareContainerMarginRight = 0;
    
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.shareContainerGradientLeftWidth = 0;
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.isShareContainerHaveGradient = NO;
    
    [UMSocialShareUIConfig shareInstance].shareTitleViewConfig.isShow = NO;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxRowCountForPortraitAndBottom = 2;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxColumnCountForPortraitAndBottom = 5;

    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewBackgroundColor = [UIColor whiteColor];
     [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageBGColor = [UIColor whiteColor];
    [UMSocialShareUIConfig shareInstance].shareCancelControlConfig.shareCancelControlBackgroundColor = [UIColor whiteColor];
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPadingBottom = 4;
    
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxColumnCountForLandscapeAndBottom = 4;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxColumnCountForPortraitAndBottom = 4;
    
    [UMSocialShareUIConfig shareInstance].shareCancelControlConfig.shareCancelControlText = @"取消";
    
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine),@(UMSocialPlatformType_Qzone),@(UMSocialPlatformType_QQ)]];
    
//    ,@(UMSocialPlatformType_Sina)

    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {

        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:content thumImage:[UIImage imageNamed:@"gywm_logo"]];
        shareObject.webpageUrl = url;

        messageObject.shareObject = shareObject;

        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:[UIApplication sharedApplication].keyWindow.rootViewController completion:^(id result, NSError *error) {

            if (error) {
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
            }else{
                if ([result isKindOfClass:[UMSocialShareResponse class]]) {
                    UMSocialShareResponse *resp = result;
                    UMSocialLogInfo(@"response message is %@",resp.message);
                    UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                }else{
                    UMSocialLogInfo(@"response data is %@",result);
                }
            }
        }];
    }];
}

#pragma mark -  分享 含回调
- (void)shareInfoWithImage:(id)image WithTitle:(NSString *)title content:(NSString *)content url:(NSString *)url completion:(UMSocialRequestCompletionHandler)completion
{
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine)]];

    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {

        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        UIImage*shareimage=nil;
        if ([image isKindOfClass:[UIImage class]]) {
            shareimage = image;
        }else if ([image isKindOfClass:[NSString class]]) {
            shareimage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:image]]];
        }
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:content thumImage:shareimage];
        shareObject.webpageUrl = url;

        messageObject.shareObject = shareObject;

        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:[UIApplication sharedApplication].keyWindow.rootViewController completion:completion];
    }];
}

#pragma mark -  分享 含回调
- (void)shareInfoWithTitle:(NSString *)title content:(NSString *)content url:(NSString *)url completion:(UMSocialRequestCompletionHandler)completion
{
    // 自定义面板
    [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewBackgroundColor = [UIColor dc_colorWithHexString:@"#E6E6E6"];
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.shareContainerBackgroundColor = [UIColor dc_colorWithHexString:@"#E6E6E6"];
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.shareContainerMarginLeft = 0;
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.shareContainerMarginRight = 0;
    
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.shareContainerGradientLeftWidth = 0;
    [UMSocialShareUIConfig shareInstance].shareContainerConfig.isShareContainerHaveGradient = NO;
    
    [UMSocialShareUIConfig shareInstance].shareTitleViewConfig.isShow = NO;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxRowCountForPortraitAndBottom = 2;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxColumnCountForPortraitAndBottom = 5;

    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewBackgroundColor = [UIColor whiteColor];
     [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageBGColor = [UIColor whiteColor];
    [UMSocialShareUIConfig shareInstance].shareCancelControlConfig.shareCancelControlBackgroundColor = [UIColor whiteColor];
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPadingBottom = 4;
    
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxColumnCountForLandscapeAndBottom = 4;
    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageMaxColumnCountForPortraitAndBottom = 4;
    
    [UMSocialShareUIConfig shareInstance].shareCancelControlConfig.shareCancelControlText = @"取消";
    
//    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine),@(UMSocialPlatformType_Qzone),@(UMSocialPlatformType_QQ)]];
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_WechatTimeLine)]];
    

    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {

        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:content thumImage:[UIImage imageNamed:@"gywm_logo"]];
        shareObject.webpageUrl = url;

        messageObject.shareObject = shareObject;

        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:[UIApplication sharedApplication].keyWindow.rootViewController completion:completion];
    }];
}


#pragma mark -  分享 微信小程序
- (void)dc_shareWeChatPlatformType:(UMSocialPlatformType)platformType
{
    //    //创建分享消息对象
    //    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    //    UMShareMiniProgramObject *shareObject = [UMShareMiniProgramObject shareObjectWithTitle:@"小程序标题" descr:@"小程序内容描述" thumImage:[UIImage imageNamed:@"gywm_logo"]];
    //
    //    shareObject.webpageUrl = @"https://www.baidu.com";
    //    shareObject.userName = @"猎手U聘";
    //    shareObject.path = @"pages/job/job?id=123455";
    //    messageObject.shareObject = shareObject;
    ////    shareObject.hdImageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"111111111" ofType:@"png"]];
    //    shareObject.miniProgramType = UShareWXMiniProgramTypePreview; // 可选体验版和开发板
    //    //调用分享接口
    //    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
    //
    //        if (error) {
    //            NSLog(@"error - %@",error);
    //            UMSocialLogInfo(@"************Share fail with error %@*********",error);
    //        }else{
    //            if ([data isKindOfClass:[UMSocialShareResponse class]]) {
    //                UMSocialShareResponse *resp = data;
    //                //分享结果消息
    //                UMSocialLogInfo(@"response message is %@",resp.message);
    //                //第三方原始返回的数据
    //                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
    //            }else{
    //                UMSocialLogInfo(@"response data is %@",data);
    //            }
    //
    //            NSLog(@"data - %@",data);
    //        }
    //    }];
}


#pragma mark - 微信登录 
- (void)dc_umWechatLogin:(void(^)(UMSocialUserInfoResponse * resp))successBlcok
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {

        if (error) {

            NSLog(@"error - %@",error);

        } else {
            UMSocialUserInfoResponse *resp = result;
            // 授权信息
            NSLog(@"Wechat uid: %@", resp.uid);
            NSLog(@"Wechat openid: %@", resp.openid);
            NSLog(@"Wechat unionid: %@", resp.unionId);
            NSLog(@"Wechat accessToken: %@", resp.accessToken);
            NSLog(@"Wechat refreshToken: %@", resp.refreshToken);
            NSLog(@"Wechat expiration: %@", resp.expiration);
            // 用户信息
            NSLog(@"Wechat name: %@", resp.name);
            NSLog(@"Wechat iconurl: %@", resp.iconurl);
            NSLog(@"Wechat gender: %@", resp.unionGender);
            // 第三方平台SDK源数据
            NSLog(@"Wechat originalResponse: %@", resp.originalResponse);

            successBlcok(resp);
        }
    }];
}


#pragma mark - QQ登录
- (void)dc_umQQLogin:(void(^)(UMSocialUserInfoResponse * resp))successBlcok
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_QQ currentViewController:nil completion:^(id result, NSError *error) {

        if (error) {

            NSLog(@"error - %@",error);

        } else {
            UMSocialUserInfoResponse *resp = result;
            // 授权信息
            NSLog(@"qq uid: %@", resp.uid);
            NSLog(@"qq openid: %@", resp.openid);
            NSLog(@"qq unionid: %@", resp.unionId);
            NSLog(@"qq accessToken: %@", resp.accessToken);
            NSLog(@"qq refreshToken: %@", resp.refreshToken);
            NSLog(@"qq expiration: %@", resp.expiration);
            // 用户信息
            NSLog(@"qq name: %@", resp.name);
            NSLog(@"qq iconurl: %@", resp.iconurl);
            NSLog(@"qq gender: %@", resp.unionGender);
            // 第三方平台SDK源数据
            NSLog(@"qq originalResponse: %@", resp.originalResponse);

            successBlcok(resp);
        }
    }];
}


#pragma mark -  微信登录 原生
- (void)dc_weixinAuthReq
{
//    if([WXApi isWXAppInstalled]){//判断用户是否已安装微信App
//
//        SendAuthReq *req = [[SendAuthReq alloc] init];
//        req.state = @"wx_oauth_authorization_state";//用于保持请求和回调的状态，授权请求或原样带回
//        req.scope = @"snsapi_userinfo";//授权作用域：获取用户个人信息
//
//        [WXApi sendReq:req];//发起微信授权请求
//
//    }else{
//        
//        //提示：未安装微信应用或版本过低
//        [self showNoWxTip];
//    }
}

@end


#endif
